package com.lan.java8.model.info;

import com.lan.java8.antlr.Java8Parser;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;


@Data
public class PackageRoot {

    private String var = "package";

    private String name;

    private List<ImportGen> imports;

    private List<ClassInfo> classInfos;

    private List<InterfaceInfo> interfaceInfos;


    public void start(Java8Parser.CompilationUnitContext context) {
        Java8Parser.PackageDeclarationContext packageDeclarationContext = context.packageDeclaration();
        PackageDeclarationContext(packageDeclarationContext);
        List<Java8Parser.ImportDeclarationContext> importDeclarationContextList = context.importDeclaration();
        if (importDeclarationContextList != null && importDeclarationContextList.size() > 0) {
            List<ImportGen> ipList = new ArrayList<>();
            for (int i = 0; i < importDeclarationContextList.size(); i++) {
                Java8Parser.ImportDeclarationContext item = importDeclarationContextList.get(i);
                ImportGen o = new ImportGen();
                o.start(item);
                ipList.add(o);
            }
            this.setImports(ipList);
        }

        List<Java8Parser.TypeDeclarationContext> typeDeclarationContextList = context.typeDeclaration();
        if (typeDeclarationContextList != null && typeDeclarationContextList.size() > 0) {

            List<ClassInfo> classInfoList = new ArrayList<>();
            List<InterfaceInfo> interfaceInfoList = new ArrayList<>();
            for (int i = 0; i < typeDeclarationContextList.size(); i++) {
                Java8Parser.TypeDeclarationContext typeDeclarationContext = typeDeclarationContextList.get(i);
                Java8Parser.ClassDeclarationContext classDeclarationContext = typeDeclarationContext.classDeclaration();
                if (classDeclarationContext != null) {
                    Java8Parser.NormalClassDeclarationContext normalClassDeclarationContext = classDeclarationContext.normalClassDeclaration();
                    if (normalClassDeclarationContext != null) {
                        ClassInfo info = new ClassInfo();
                        info.start(normalClassDeclarationContext);
                    }
                }

                Java8Parser.InterfaceDeclarationContext interfaceDeclarationContext = typeDeclarationContext.interfaceDeclaration();
                if (interfaceDeclarationContext != null) {
                    InterfaceInfo info = new InterfaceInfo();
                    info.start(interfaceDeclarationContext);
                }

            }
        }
    }


    private void PackageDeclarationContext(Java8Parser.PackageDeclarationContext packageDeclarationContext) {
//        System.out.println(packageDeclarationContext.getText());
        if (packageDeclarationContext == null) {
            return;
        }
        Java8Parser.PackageNameContext packageNameContext = packageDeclarationContext.packageName();
        if (packageNameContext != null) {
            this.name = packageNameContext.getText();
        }

//        List<Java8Parser.PackageModifierContext> modifierContextList = packageDeclarationContext.packageModifier();
//        if (modifierContextList != null && modifierContextList.size() > 0) {
//            for (int i = 0; i < modifierContextList.size(); i++) {
//                Java8Parser.PackageModifierContext modifierContext = modifierContextList.get(i);
//                PackageModifierContext(modifierContext);
//            }
//        }
//        Java8Parser.PackageNameContext packageNameContext = packageDeclarationContext.packageName();
//        PackageDeclarationContext(packageNameContext);
//        while (token != null && token.getType() != Token.EOF) {
//            System.out.println(token.getText());
//            TokenSource tokenSource = token.getTokenSource();
//            if (tokenSource != null) {
//                token = tokenSource.nextToken();
//            }
//        }
    }

//    private void PackageDeclarationContext(Java8Parser.PackageNameContext packageNameContext) {
//        if (packageNameContext != null) {
//            System.out.println("PackageDeclarationContext " + packageNameContext.getText());
//        }
//    }
//
//    private void PackageModifierContext(Java8Parser.PackageModifierContext modifierContext) {
//        if (modifierContext != null) {
//            System.out.println("PackageModifierContext: " + modifierContext.getText());
//            Java8Parser.AnnotationContext annotationContext = modifierContext.annotation();
//            AnnotationContext(annotationContext);
//        }
//    }
//
//    private void AnnotationContext(Java8Parser.AnnotationContext annotationContext) {
//        if (annotationContext != null) {
//            System.out.println("AnnotationContext: " + annotationContext.getText());
//            annotationContext.markerAnnotation();
//        }
//    }

}
